﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace EmployeeCheckIn
{
	public partial class EditDataEmp : System.Web.UI.Page
	{
        protected void Page_Load(object sender, EventArgs e)
        {
            EmpEntity load = new EmpEntity();
            DataTable dt = new DataTable();
            dt = (DataTable)Session["EditData"];
            if (!IsPostBack)
            {
                   if (dt != null && dt.Rows.Count > 0)
            {
                hdf1.Value = dt.Rows[0]["EMP_ID"].ToString();
                txtEMP_CODE.Text = dt.Rows[0]["emp_code"].ToString();
                rbltitle.SelectedValue = dt.Rows[0]["emp_title"].ToString();
                txtEMP_FIRSTNAME.Text = dt.Rows[0]["emp_firstname"].ToString();
                txtEMP_LASTNAME.Text = dt.Rows[0]["emp_lastname"].ToString();
                ddlposition2.SelectedValue = dt.Rows[0]["emp_position_id"].ToString();
                txtEMP_START_DATE.Text = Convert.ToDateTime(dt.Rows[0]["emp_start_date"].ToString()).ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("th-TH"));
                txtEMP_BIRTH_DATE.Text = Convert.ToDateTime(dt.Rows[0]["emp_birth_date"].ToString()).ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("th-TH"));
                txtEMP_ID_CARD.Text = dt.Rows[0]["emp_id_card"].ToString();
                txtEMP_ADDRESS.Text = dt.Rows[0]["emp_address"].ToString();
                ddlProvince.SelectedValue = dt.Rows[0]["emp_province_id"].ToString();
                txtEMP_POST_CODE.Text = dt.Rows[0]["emp_post_code"].ToString();
                txtEMP_PHONE.Text = dt.Rows[0]["emp_phone"].ToString();
                ddlStatus2.SelectedValue = dt.Rows[0]["emp_status_id"].ToString();
                imEmp.ImageUrl = dt.Rows[0]["emp_pic"].ToString();
                //lbNotFound.Visible = false;
             }
            }
            

        }

        public void dataGridDetail(string i)
        {
            if (Session["User"] != null && Session["Pass"] != null)
            {

            Selectdata sl = new Selectdata();
            DataTable dt = sl.SelectAll(i);

            if (dt != null && dt.Rows.Count > 0)
            {
                hdf1.Value = dt.Rows[0]["EMP_ID"].ToString();
                txtEMP_CODE.Text = dt.Rows[0]["emp_code"].ToString();
                rbltitle.SelectedValue = dt.Rows[0]["emp_title"].ToString();
                txtEMP_FIRSTNAME.Text = dt.Rows[0]["emp_firstname"].ToString();
                txtEMP_LASTNAME.Text = dt.Rows[0]["emp_lastname"].ToString();
                ddlposition2.SelectedValue = dt.Rows[0]["emp_position_id"].ToString();
                txtEMP_START_DATE.Text = Convert.ToDateTime(dt.Rows[0]["emp_start_date"].ToString()).ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("th-TH"));
                txtEMP_BIRTH_DATE.Text = Convert.ToDateTime(dt.Rows[0]["emp_birth_date"].ToString()).ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("th-TH"));
                txtEMP_ID_CARD.Text = dt.Rows[0]["emp_id_card"].ToString();
                txtEMP_ADDRESS.Text = dt.Rows[0]["emp_address"].ToString();
                ddlProvince.SelectedValue = dt.Rows[0]["emp_province_id"].ToString();
                txtEMP_POST_CODE.Text = dt.Rows[0]["emp_post_code"].ToString();
                txtEMP_PHONE.Text = dt.Rows[0]["emp_phone"].ToString();
                ddlStatus2.SelectedValue = dt.Rows[0]["emp_status_id"].ToString();
                imEmp.ImageUrl = dt.Rows[0]["emp_pic"].ToString();
             //   lbNotFound.Visible = false;
            }
            //else
            //{
            //    lbNotFound.Visible = true;
            //}
            }
            else
            {
                Response.Redirect("Default.aspx");
            }

        }
        public DateTime ConvestToDate(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                try
                {
                    string[] stemp = s.Split('-');
                    day = Convert.ToInt32(stemp[0]);
                    mount = Convert.ToInt32(stemp[1]);
                    years = Convert.ToInt32(stemp[2]);
                }
                catch (Exception)
                {

                    string[] stemp = s.Split('/');
                    day = Convert.ToInt32(stemp[0]);
                    mount = Convert.ToInt32(stemp[1]);
                    years = Convert.ToInt32(stemp[2]);
                }


                if (years > 2300)
                {
                    years = years - 543;
                }
                return new DateTime(years, mount, day);
            }
            catch (Exception)
            {
                return DateTime.Now;
                //lbNotSDate.Visible = true;
                //lbNotSDate.Text = "*รูปแบบ วว/ดด/ปปปป";
                //lbNotBDate.Visible = true;
                //lbNotBDate.Text = "*รูปแบบ วว/ดด/ปปปป";
            }

            return new DateTime(years, mount, day);

        }
        public bool CheckConvestToDate(string s)
        {
            int day;
            int mount;
            int years;
            bool resault = false;
            try
            {
                try
                {
                    string[] stemp = s.Split('-');
                    day = Convert.ToInt32(stemp[0]);
                    mount = Convert.ToInt32(stemp[1]);
                    years = Convert.ToInt32(stemp[2]);
                }
                catch (Exception)
                {

                    string[] stemp = s.Split('/');
                    day = Convert.ToInt32(stemp[0]);
                    mount = Convert.ToInt32(stemp[1]);
                    years = Convert.ToInt32(stemp[2]);
                }


                if (years > 2300)
                {
                    years = years - 543;
                }
                return resault = true;
            }
            catch (Exception)
            {

                resault = false;
            }
            return resault;

        }

        private bool checknumber(string s)
        {
            bool result = false;
            try
            {
                
                Convert.ToDouble(s);
                result = true;
            }
            catch (Exception)
            {
                result = false;

            }
            return result;
        }






        private void lbHide()
        {
            lbNotFName.Visible = false;
            lbNotIDCard.Visible = false;
            lbNotLName.Visible = false;
            lbNotPhone.Visible = false;
            lbNotPost.Visible = false;
            lbUpsucsess.Visible = false;
            lbNotBDate.Visible = false;
            lbNotSDate.Visible = false;
        
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //if (Session["User"] != null && Session["Pass"] != null)
            //{
            //    string i = txtFind.Text;
            //    dataGridDetail(i);
            //}
            //else
            //{
            //    Response.Redirect("Default.aspx");
            //}
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Session["User"] != null && Session["Pass"] != null)
            {
                lbHide();
                UpdateData up = new UpdateData();
                EmpEntity emp = new EmpEntity();
                StringBuilder sb = new StringBuilder();


                bool result = false;
                bool submit = true;
                emp.ID = Convert.ToInt32(hdf1.Value);
                emp.EmpId = txtEMP_CODE.Text;
                emp.Prefix = Convert.ToInt32(rbltitle.SelectedValue);
                emp.FirstName = txtEMP_FIRSTNAME.Text;
                emp.LastName = txtEMP_LASTNAME.Text;
                emp.Position = Convert.ToInt32(ddlposition2.SelectedValue);
                DateTime dtmBirth = ConvestToDate(txtEMP_BIRTH_DATE.Text);
                DateTime dtmStart = ConvestToDate(txtEMP_START_DATE.Text);
                emp.DateBirth = dtmBirth;
                emp.DateStart = dtmStart;
                emp.CardID = txtEMP_ID_CARD.Text;
                emp.Address = txtEMP_ADDRESS.Text;
                emp.City = Convert.ToInt32(ddlProvince.SelectedValue);
                emp.PostID = txtEMP_POST_CODE.Text;
                emp.Phone = txtEMP_PHONE.Text;
                emp.status = Convert.ToInt32(ddlStatus2.SelectedValue);
                emp.EmpPic = imEmp.ImageUrl;

                if (txtEMP_FIRSTNAME.Text == "")
                {
                    lbNotFName.Visible = true;
                    lbNotFName.Text = "*กรุณากรอกชื่อ";
                    txtEMP_FIRSTNAME.Focus();
                    result = true;
                    submit = false;
                }
                if (txtEMP_LASTNAME.Text == "")
                {
                    lbNotLName.Visible = true;
                    lbNotLName.Text = "*กรุณากรอกนามสกุล";
                    result = true;
                    submit = false;
                }
               

                if (checknumber(txtEMP_POST_CODE.Text) == false || (txtEMP_POST_CODE.Text == ""))
                {
                    //lbNotPost.Visible = true;
                    //lbNotPost.Text = "*กรุณากรอกข้อมูล เป็นตัวเลข";
                    //result = true;
                    //submit = false;
                }

                if (CheckConvestToDate(txtEMP_START_DATE.Text) == false || (txtEMP_START_DATE.Text == ""))
                {
                    //lbNotSDate.Visible = true;
                    //lbNotSDate.Text = "*รูปแบบ วว/ดด/ปปปป";
                    //result = true;
                    //submit = false;
                }

                if (CheckConvestToDate(txtEMP_BIRTH_DATE.Text) == false || (txtEMP_BIRTH_DATE.Text == ""))
                {
                    //lbNotBDate.Visible = true;
                    //lbNotBDate.Text = "*รูปแบบ วว/ดด/ปปปป";
                    //result = true;
                    //submit = false;
                }
                

                if (checknumber(txtEMP_PHONE.Text) == false || (txtEMP_PHONE.Text == ""))
                {
                //    lbNotPhone.Visible = true;
                //    lbNotPhone.Text = "*กรุณากรอกข้อมูล เป็นตัวเลข";
                //    result = true;
                //    submit = false;
                }
               

                if ((checknumber(txtEMP_ID_CARD.Text) == false && (txtEMP_ID_CARD.Text != "")))
                {
                    lbNotIDCard.Visible = true;
                    lbNotIDCard.Text = "*กรุณากรอกข้อมูล เป็นตัวเลข";
                    result = true;
                    submit = false;
                }
                



                if (result == true && submit == false)
                {
                    return;
                }
                if (IsPostBack)
                {

                    String fileExtension = "";
                    Boolean fileOK = false;
                    Boolean fileNotChange = false;
                    Boolean filego = false;
                    String path = Server.MapPath("~/Immages/");
                    if (imEmp.ImageUrl != null)
                    {
                        fileOK = true;
                        fileNotChange = true;
                        filego = false;
                        
                        if (upfPicture.HasFile)
                        {
                            fileExtension = System.IO.Path.GetExtension(upfPicture.FileName);
                            String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".GIF", ".PNG", ".JPEG", ".JPG" };
                            for (int j = 0; j < allowedExtensions.Length; j++)
                            {
                                if (fileExtension == allowedExtensions[j])
                                {
                                    fileOK = true;
                                    filego = true;

                                }
                                if (fileExtension != allowedExtensions[j])
                                {
                                    fileOK = false;
                                }
                            }
                            if (filego)
                            {
                                fileOK = true;
                            }
                        }

                    }
                   

                    if (fileOK&&fileNotChange)
                    {
                        try
                        {
                            emp.EmpPic = "~/Immages/" + upfPicture.FileName;
                            upfPicture.SaveAs(path + upfPicture.FileName);
                            lbUpsucsess.Text = "File uploaded!";
                        }
                        catch (Exception)
                        {
                            if (fileNotChange == true)
                            {
                                lbUpsucsess.Text = "มีรูปอยู่แล้ว";
                                emp.EmpPic = imEmp.ImageUrl;
                            }

                            else
                            {
                                result = true;
                                lbUpsucsess.Text = "ไฟล์ยังไม่ได้อัพโหลด";
                                submit = false;
                            }

                        }
                    }
                    else
                    {
                        result = true;
                        lbUpsucsess.Text = "ไม่รองรับไฟล์ชนิดนี้";
                        lbUpsucsess.Visible = true;
                        submit = false;
                    }
                    if (submit == true)
                    {
                    int i = Convert.ToInt32(hdf1.Value);
                    up.Update(emp, i);
                    Response.Write("<script>alert('แก้ไขข้อมูลเรียบร้อยแล้ว!'); window.location='ShowList_Empoyee.aspx';</script>");
                    
                     }

                }

               
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }  
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (Session["User"] != null && Session["Pass"] != null)
            {
                Response.Redirect("EditDataEmp.aspx");
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            if (Session["User"] != null && Session["Pass"] != null)
            {
                Server.Transfer("ShowList_Empoyee.aspx");
            }
            else
            {
                Response.Redirect("Default.aspx");
            }

        }


    }
}